/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IncidenciaPanel.java
 *
 * Created on 08-nov-2011, 20:48:37
 */
package edu.uoc.tdp.pac3.client.gui.panels.incidencia;

import edu.uoc.tdp.pac3.bo.impl.IncidenciaBO;
import edu.uoc.tdp.pac3.client.gui.panels.BaseIncidenciaPanel;
import edu.uoc.tdp.pac3.domain.Request;
import edu.uoc.tdp.pac3.domain.RequestType;
import java.rmi.RemoteException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Gerard
 */
public class IncidenciaPanel extends BaseIncidenciaPanel {

    private Request request;

    /** Creates new form IncidenciaPanel */
    public IncidenciaPanel() {
        super(new IncidenciaBO());
        initComponents();
        setLabels();
    }

    public IncidenciaPanel(Request request) {
        super(new IncidenciaBO());
        this.request = request;
        initComponents();
        setLabels();
    }

    /** This method is called from within the constructor to
     * initialize the form.º
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblRequestId = new javax.swing.JLabel();
        requestId = new javax.swing.JTextField();
        lblUserId = new javax.swing.JLabel();
        userId = new javax.swing.JTextField();
        lblAreaId = new javax.swing.JLabel();
        areaId = new javax.swing.JTextField();
        btnInsert = new javax.swing.JButton();
        lblCreationDate = new javax.swing.JLabel();
        creationDate = new javax.swing.JTextField();
        lblDescription = new javax.swing.JLabel();
        description = new javax.swing.JTextField();
        isRequestSolved = new javax.swing.JCheckBox();

        lblRequestId.setText("lblRequestId");
        lblRequestId.setName("lblRequestId"); // NOI18N

        requestId.setName("requestId"); // NOI18N

        lblUserId.setText("lblUserId");
        lblUserId.setName("lblUserId"); // NOI18N

        userId.setName("userId"); // NOI18N

        lblAreaId.setText("lblAreaId");
        lblAreaId.setName("lblAreaId"); // NOI18N

        areaId.setName("areaId"); // NOI18N

        btnInsert.setText("insert");
        btnInsert.setName("btnInsert"); // NOI18N
        btnInsert.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                onClick(evt);
            }
        });

        lblCreationDate.setText("lblCreationDate");
        lblCreationDate.setName("lblCreationDate"); // NOI18N

        creationDate.setName("creationDate"); // NOI18N

        lblDescription.setText("lblDescription");
        lblDescription.setName("lblDescription"); // NOI18N

        description.setName("description"); // NOI18N

        isRequestSolved.setText("isRequestSolved");
        isRequestSolved.setName("isRequestSolved"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(lblRequestId)
                    .add(lblUserId)
                    .add(lblAreaId))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(areaId)
                    .add(userId)
                    .add(requestId, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(lblCreationDate)
                            .add(lblDescription))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(description, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                            .add(creationDate, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
                        .add(200, 200, 200))
                    .add(layout.createSequentialGroup()
                        .add(isRequestSolved)
                        .addContainerGap())))
            .add(layout.createSequentialGroup()
                .add(212, 212, 212)
                .add(btnInsert)
                .addContainerGap(414, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(29, 29, 29)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblRequestId)
                    .add(requestId, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(lblCreationDate)
                    .add(creationDate, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblUserId)
                    .add(userId, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(lblDescription)
                    .add(description, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(23, 23, 23)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblAreaId)
                    .add(areaId, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(isRequestSolved))
                .add(18, 18, 18)
                .add(btnInsert)
                .addContainerGap(49, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void onClick(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_onClick

    checkFields();
        try {
            getIncidenciaBO().novaIncidencia(request);
        } catch (RemoteException ex) {
            Logger.getLogger(IncidenciaPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_onClick
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField areaId;
    private javax.swing.JButton btnInsert;
    private javax.swing.JTextField creationDate;
    private javax.swing.JTextField description;
    private javax.swing.JCheckBox isRequestSolved;
    private javax.swing.JLabel lblAreaId;
    private javax.swing.JLabel lblCreationDate;
    private javax.swing.JLabel lblDescription;
    private javax.swing.JLabel lblRequestId;
    private javax.swing.JLabel lblUserId;
    private javax.swing.JTextField requestId;
    private javax.swing.JTextField userId;
    // End of variables declaration//GEN-END:variables

    private void checkFields() {
        request = new Request();
        request.setAreaId(Integer.parseInt(areaId.getText()));
        request.setCreationDate(null);
        request.setDescription(description.getText());
        request.setId(Integer.parseInt(requestId.getText()));
        RequestType requestType = RequestType.NOTSOLVED;
        if (isRequestSolved.isSelected()) {
            requestType = RequestType.SOLVED;
        }
        request.setRequestType(requestType);
        request.setUserId(Integer.parseInt(userId.getText()));
    }

    private void fillComponents() {
        if (request != null) {
            areaId.setText(request.getAreaId().toString());
        }
    }

    @Override
    protected void setLabels() {
        lblAreaId.setText(getText("lblAreaId"));
        lblCreationDate.setText(getText("lblCreationDate"));
        lblDescription.setText(getText("lblDescription"));
        lblRequestId.setText(getText("lblRequestId"));
        lblUserId.setText(getText("lblUserId"));
        isRequestSolved.setText(getText("lblRequestSolved"));
        btnInsert.setText(getText("btnRequestInsert"));
    }
}
